<?php
include_once('top.php');


$allWorkers = fetch_all("SELECT First_name,Last_name,Email FROM USER WHERE Type='R' ORDER BY `Last_name` ASC;");
if (!empty($_POST)) {
	$title = mysql_real_escape_string($_POST['title']);
	$desc = mysql_real_escape_string($_POST['description']);
	$manager = mysql_real_escape_string($_POST['manager']);
	
	if (empty($title)) {
		$message = 'You cannot have a blank title';
	} else {
		if (!isset($_POST['manager'])){
			$id = mysql_real_escape_string($_POST['P_id']);
			$manager = fetch_only("SELECT `Manager` FROM `PROJECT` WHERE `P_id`='$id'");
			$manager = $manager['Manager'];
		
			run("UPDATE `PROJECT` SET `Title`='" . $title . "', `Description`='" . $desc . "' WHERE P_id=" . $_POST['P_id'] . ";");
			
			// First, check that all of the workers with tasks are still there
			$owners = fetch_all("SELECT `Email`, `First_name`, `Last_name` "
					. "FROM `TASK`, `USER` "
					. "WHERE `USER`.`Email`=`TASK`.`Employee` AND `Project`='$id'");
			
			$good = true;
			$violation = '';
			
			foreach ($owners as $worker) {
				if (!in_array($worker['Email'], $_POST['check']) && $worker['Email'] != $manager) {
					$good = false;
					$violation = "{$worker['First_name']} {$worker['Last_name']}";
				}
			}
			
			if ($good) {
				run("DELETE FROM PROJECT_WORKERS WHERE Project=" . $_POST['P_id'] . ";");
				
				foreach($_POST['check'] as $var => $value) {
					run("INSERT INTO PROJECT_WORKERS (Project,Employee) VALUES (" . $_POST['P_id'] . ",'" . $value . "');");
				}
				
				$_GET['project'] = $_POST['P_id'];
			} else {
				$message = "Employees with open tasks cannot be removed (violating change: removing $violation)";
			}
		} else {
			run("INSERT INTO PROJECT (Title, Description, Manager) VALUES ('$title','$desc','$manager');");
			$myProject = fetch_only("SELECT * FROM PROJECT WHERE Manager='$manager' ORDER BY P_id DESC;");
			foreach($_POST['check'] as $var => $value) {
				run("INSERT INTO PROJECT_WORKERS (Project,Employee) VALUES (" . $myProject['P_id'] . ",'" . $value . "');");
			}
			$_GET['project'] = $myProject['P_id'];
		}
	}
}

if (isset($_GET['project'])){
	$currProject = fetch_only("SELECT Title, Description, P_id FROM PROJECT WHERE P_id=" . $_GET['project'] . ";");
	$projectManager = fetch_only("SELECT First_name , Last_name, Email
	FROM USER RIGHT JOIN (
	SELECT Manager
	FROM PROJECT
	WHERE P_id=" . $_GET['project'] . "
	) as P ON USER.Email=P.Manager;");
	$projectWorkers = fetch_all("SELECT DISTINCT First_name , Last_name , Email
	FROM (
	USER INNER JOIN PROJECT_WORKERS
	ON USER.Email=PROJECT_WORKERS.Employee
	) WHERE Project=" . $_GET['project'] . ";");
	} else {
	$projectManager = 0;
}

$user = fetch_only("SELECT First_name, Last_name, Email FROM USER WHERE Email='{$_GET['manager']}'");

$workersTasks = fetch_all("SELECT COUNT(T.Name) as totalTasks,Last_name FROM (SELECT * FROM USER WHERE Type='R') AS U LEFT JOIN (SELECT * FROM TASK WHERE `Project`='{$_GET['project']}') AS T ON (Email=Employee) GROUP BY U.Email ORDER BY U.Last_name ASC");
?>

<html>
<head>
	<title>GT Project Manager - Project Info</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<link rel="stylesheet" type="text/css" href="inner.css" />
</head>
<body>
	<table cellspacing="0" cellpadding="0" id="main">
		<tr><td id="title">
			Project Info
			<div class="nav">
				<a href="login.php?action=logout">Log Out</a><br />
				<?php echo $link ?><br />
				<?php
				if (isset($_GET['project'])) {
				?>
				<a href="project_milestones.php?project=<?php echo $_GET['project']; ?>">Project Milestones</a>
				<?php
				}
				?>
			</div>
		</td></tr>
		<?php
		if ($message) {
		?>
		<tr><td class="message" colspan="2"><?php echo $message ?></td></tr>
		<?php
		}
		?>
		<tr>
			<td>
			<form action="#" method="POST">
				<table class="details" cellspacing="0" cellpadding="0">
				<?php
				echo '
					<tr>
						<th>Project ID</th>
						<td>' . $currProject['P_id'] . '<input type="hidden" name="P_id" value="' . $currProject['P_id'] . '"/></td>
					</tr>
					<tr>
						<th>Project Title</th>
						<td><input type="text" name="title" value="' . $currProject['Title'] . '" /></td>
					</tr>
					<tr>
						<th>Description</th>
						<td>
							<textarea type="text" name="description" resizable="false">' . $currProject['Description'] . '</textarea>
						</td>
					</tr>
					<tr>
						<th>Project Manager</th>';
						if (empty($_GET['manager'])){
						echo '<td>' . $projectManager['First_name'] . ' ' . $projectManager['Last_name'] . '</td>
						</tr>';
						} else {
						echo '<td>' . $user['First_name'] . ' ' . $user['Last_name'] . '</td>
						</tr><input type="hidden" name="manager" value="' . $_GET['manager'] . '"/>';
						}
					?>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="subsection boxes" cellspacing="0" cellpadding="0">
					<tr><td class="title" colspan="3">
						Project Team
						<hr size="1" />
					</td></tr>
					<tr><td class="description" colspan="3">
						Check the box next to the name of each team member.
					</td></tr>
					<?php
					for ($i=0; $i < count($allWorkers); $i++){
					if (!($allWorkers[$i]['Email'] == $user['Email'] || $allWorkers[$i]['Email']==$projectManager['Email'])){
						if (in_array($allWorkers[$i],$projectWorkers)) {
							if ($workersTasks[$i]['totalTasks']>0) {
								echo '
							<tr>
								<td><input type="checkbox" name="check[]" value="' . $allWorkers[$i]['Email'] . '" checked="true" /></td>
								<td><a href="view_employee.php?user=' . $allWorkers[$i]['Email'] . '">' . $allWorkers[$i]['First_name'] . ' ' . $allWorkers[$i]['Last_name'] . '</a></td>
								<td>' . $allWorkers[$i]['Email'] . '</td>
							</tr>';
							} else {
							echo '
							<tr>
								<td><input type="checkbox" name="check[]" value="' . $allWorkers[$i]['Email'] . '" checked="true" /></td>
								<td><a href="view_employee.php?user=' . $allWorkers[$i]['Email'] . '">' . $allWorkers[$i]['First_name'] . ' ' . $allWorkers[$i]['Last_name'] . '</a></td>
								<td>' . $allWorkers[$i]['Email'] . '</td>
							</tr>';}
						} else {
							echo '
							<tr>
								<td><input type="checkbox" name="check[]" value="' . $allWorkers[$i]['Email'] . '"/></td>
								<td><a href="view_employee.php?user=' . $allWorkers[$i]['Email'] . '">' . $allWorkers[$i]['First_name'] . ' ' . $allWorkers[$i]['Last_name'] . '</a></td>
								<td>' . $allWorkers[$i]['Email'] . '</td>
							</tr>';
						}
					}
					}
					?>

				</table>
			</td>
		</tr>
		<tr>
			<td class="button_bar">
				<input type="submit" value="Save" />
			</td>
		</tr>
	</table>
	</form>
</body>
</html>
